#!/bin/bash
# chkconfig:2345 30 70
# description: Mongo database
# precessname: mongod

mongodb_dir={{ basedir }}

config_datadir={{ config_data_dir }}
shard1_datadir={{ shard1_data_dir }}
shard2_datadir={{ shard2_data_dir }}
shard3_datadir={{ shard3_data_dir }}

config_configfile={{ conf_dir }}/{{ config_name }}.conf
mongos_configfile={{ conf_dir }}/{{ mongos_name }}.conf
shard1_configfile={{ conf_dir }}/{{ shard1_name }}.conf
shard2_configfile={{ conf_dir }}/{{ shard2_name }}.conf
shard3_configfile={{ conf_dir }}/{{ shard3_name }}.conf

mongos_pidfile={{ mongos_logdir }}/{{ mongos_name }}.pid
config_pidfile={{ config_log_dir }}/{{ config_name }}.pid 
shard1_pidfile={{ shard1_log_dir }}/{{ shard1_name }}.pid
shard2_pidfile={{ shard2_log_dir }}/{{ shard2_name }}.pid
shard3_pidfile={{ shard3_log_dir }}/{{ shard3_name }}.pid

function start-config(){
    #print the tips
    echo -n $"Starting mongod of config_server:"
    su - mongodb -c "$mongodb_dir/bin/mongod -f $config_configfile"
    #get the result
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        touch $config_datadir/mongod.lock
    fi 
}

function stop-config(){
    #print the tips
    echo  $"stopping mongod of config_server:"
    su - mongodb -c "$mongodb_dir/bin/mongod --shutdown --dbpath $config_datadir"
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        rm -f $config_datadir/mongod.lock
        rm -f $config_pidfile
    fi 
}

function start-mongos(){
    #print the tips
    echo  $"Starting mongod of mongos_server:"
    su - mongodb -c "$mongodb_dir/bin/mongos -f $mongos_configfile"
}

function stop-mongos(){
    echo  $"stopping mongod of mongos_server:"
    kill `cat $mongos_pidfile`
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        rm -f $mongos_pidfile
    fi 
}

function start-shard1(){
    #print the tips
    echo  $"Starting mongod of shard1_server:"
    su - mongodb -c "$mongodb_dir/bin/mongod -f $shard1_configfile"
    #get the result
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        $shard1_datadir/mongod.lock
    fi
}

function stop-shard1(){
    echo  $"stopping mongod of shard1_server:"
    su - mongodb -c "$mongodb_dir/bin/mongod --shutdown --dbpath $shard1_datadir"
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        rm -f $shard1_datadir/mongod.lock
        rm -f $shard1_pidfile
    fi
}

function start-shard2(){
    #print the tips
    echo  $"Starting mongod of shard2_server:"
    su - mongodb -c "$mongodb_dir/bin/mongod -f $shard2_configfile"
    #get the result
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        $shard2_datadir/mongod.lock
    fi
}

function stop-shard2(){
    echo  $"stopping mongod of shard2_server:"
    su - mongodb -c "$mongodb_dir/bin/mongod --shutdown --dbpath $shard2_datadir"
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        rm -f $shard2_datadir/mongod.lock
        rm -f $shard2_pidfile
    fi
}

function start-shard3(){
    #print the tips
    echo  $"Starting mongod of shard3_server:"
    su - mongodb -c "$mongodb_dir/bin/mongod -f $shard3_configfile"
    #get the result
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        $shard3_datadir/mongod.lock
    fi
}

function stop-shard3(){
    echo  $"stopping mongod of shard3_server:"
    su - mongodb -c "$mongodb_dir/bin/mongod --shutdown --dbpath $shard3_datadir"
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        rm -f $shard3_datadir/mongod.lock
        rm -f $shard3_pidfile
    fi
}
RETVAL=0
case "$1" in
    start)
      start-config
      start-shard1
      start-shard2
      start-shard3
      start-mongos
      ;;
    stop)
      stop-mongos
      stop-shard1
      stop-shard2
      stop-shard3
      stop-config
      ;;
    restart)
      $0 stop
      $0 start 
      ;;
esac
exit $RETVAL
